using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ltf.Remoting.Operations;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.Diffs.MergeCommands
{
	public class MergeDeleteFileCommand : MergeCommand
	{
		public MergeDeleteFileCommand(TFServerConnection connection, string targetFile)
			: this(connection, targetFile, false)
		{ }

		public MergeDeleteFileCommand(TFServerConnection connection, string targetFile, bool backup)
		{
			this.Connection = connection;
			this.TargetFile = targetFile;
			this.backup = backup;
		}

		public override MergeResult Execute()
		{
			if (backup)
				BackupTargetFile();
			// set the attributes to clear any readonly flags. Lizard does not respect readonly flags.
			File.SetAttributes(TargetFile, FileAttributes.Normal);
			WorkspaceOperators wso = GetWorkspaceOperators();
			if (wso != null)
			{
				wso.PendDelete(new string[] { TargetFile });
			}
			else
			{
				File.Delete(TargetFile);
			}
			return new MergeResult(0, 0, 0, 0, 1, 0);
		}

		private bool backup;

		public bool Backup
		{
			get { return backup; }
			set { backup = value; }
		}

		public override void Validate()
		{
			if (!File.Exists(TargetFile))
				throw new MergeException(string.Format("Target file '{0}' does not exist.", TargetFile));
		}

		public override string ShortName
		{
			get { return "Delete"; }
		}
	}
}
